package client.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class InitDialog extends JDialog implements ActionListener { //, Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	
	private String host;
	private Integer port;
	private Font fontBig;
	private Font fontSmall;
	
	private JTextField portTextField;
	private JTextField hostTextField;

	/**
	 * Create the dialog.
	 * @param port 
	 * @param host 
	 */
	public InitDialog() {
		this.host = null;
		this.port = 0;
		
		fontBig = new Font("MS PGothic", Font.BOLD | Font.ITALIC, 16);
		fontSmall = new Font("Times New Roman", Font.PLAIN, 14);
		
		initGUI();
		setVisible(true);
	}
	
	/**
	 * Initialize the GUI
	 */
	public void initGUI() {

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("ICM initial settings");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
		{
			contentPanel.add(Box.createVerticalStrut(15));
			
			JPanel promptPanel = new JPanel();
			contentPanel.add(promptPanel);
			promptPanel.setLayout(new BoxLayout(promptPanel, BoxLayout.X_AXIS));
			
			JLabel promptLbl = new JLabel("Please choose the host and port of the remote server");
			promptPanel.add(promptLbl);
			promptLbl.setFont(fontBig);
			
			contentPanel.add(Box.createVerticalStrut(15));
			
			JPanel hostPanel = new JPanel();
			contentPanel.add(hostPanel);
			hostPanel.setLayout(new BoxLayout(hostPanel, BoxLayout.LINE_AXIS));
			
			hostPanel.add(Box.createHorizontalStrut(15));

			hostPanel.add(Box.createHorizontalGlue());
			
			JLabel hostLbl = new JLabel("Insert hostname:");
			hostLbl.setFont(fontSmall);
			hostPanel.add(hostLbl);
			
			hostPanel.add(Box.createHorizontalGlue());
			
			hostTextField = new JTextField();
			hostTextField.setFont(fontSmall);
			hostTextField.setToolTipText("Insert the remote computer name / IP");
			hostTextField.setMaximumSize(new Dimension(300, 50));
			hostPanel.add(hostTextField);
			hostTextField.setColumns(20);

			hostPanel.add(Box.createHorizontalGlue());
			
			hostPanel.add(Box.createHorizontalStrut(15));
			
			contentPanel.add(Box.createVerticalStrut(15));
			
			JPanel portPanel = new JPanel();
			contentPanel.add(portPanel);
			portPanel.setLayout(new BoxLayout(portPanel, BoxLayout.LINE_AXIS));
			
			portPanel.add(Box.createHorizontalStrut(15));

			portPanel.add(Box.createHorizontalGlue());
			
			JLabel portLbl = new JLabel("Insert port number:");
			portLbl.setFont(fontSmall);
			portPanel.add(portLbl);
			
			portPanel.add(Box.createHorizontalGlue());
			
			portTextField = new JTextField();
			portTextField.setToolTipText("Insert the server's port");
			portTextField.setFont(UIManager.getFont("TextField.font"));
			portTextField.setMaximumSize(new Dimension(300, 50));
			portPanel.add(portTextField);
			portTextField.setColumns(20);

			portPanel.add(Box.createHorizontalGlue());
			
			portPanel.add(Box.createHorizontalStrut(15));
			
			contentPanel.add(Box.createVerticalStrut(15));
			
		}
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		{
			JButton okButton = new JButton("OK");
			okButton.addActionListener(this);
			okButton.setActionCommand("OK");
			buttonPane.add(okButton);
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("Cancel");
			cancelButton.setActionCommand("Cancel");
			buttonPane.add(cancelButton);
		}
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getActionCommand().equals("OK")) {
			host = hostTextField.getText();
			port = Integer.parseInt(portTextField.getText());
			if(port < 1030 | port > 65536) {
				JOptionPane.showMessageDialog(this, "Non-valid port number", "Input error", JOptionPane.ERROR_MESSAGE);
			}
			else if(host.contains(" ")) {
				JOptionPane.showMessageDialog(this, "Non-valid hostname", "Input error", JOptionPane.ERROR_MESSAGE);
			}
			else 
				this.dispose();
		}
		else if(ae.getActionCommand().equals("Cancel")) {
			this.dispose();
		}
	}

	/**
	 * Returns the host name
	 * @return String
	 */
	public String getHost() {
		return host;
	}

	/**
	 * Returns the port number
	 * @return Integer
	 */
	public Integer getPort() {
		return port;
	}

	

}
